home *** CD-ROM | disk | FTP | other *** search
/ ShareWare OnLine 2 / ShareWare OnLine Volume 2 (CMS Software)(1993).iso / prog / lzw4c12.zip / RW_IO.C < prev    next >
Text File  |  1993-02-22  |  3KB  |  119 lines

  1. /*   RW_IO.C
  2. **
  3. **   Reader/Writer Buffered I/O
  4. **
  5. **   Reader() and Writer() are called directly by the LZW4C.ASM code.
  6. **   They should never be called by your application code.
  7. **
  8. **   The other functions are never called by the LZW4C.ASM code, but are
  9. *    called only by your application routines.
  10. **
  11. **   Note that only a Reader() and Writer() function is required by the
  12. **   LZW4C.ASM code. This means that you have complete control over data
  13. **   coming into and out of the compression/expansion code. Instead of
  14. **   reading or writing to disk, you can just as easily read/write to a
  15. **   buffer, serial port, etc. You just have to write the Reader() and
  16. **   Writer() code.
  17. */
  18.  
  19.  
  20. #include <stdio.h>
  21. #include "RW_IO.H"
  22.  
  23. #define FALSE 0
  24. #define TRUE !FALSE
  25.  
  26. #define BUFFER_SIZE 2048
  27.  
  28. typedef struct IOstruct
  29. {FILE *FilePtr;  /* file ptr */
  30.  char Buffer[BUFFER_SIZE];
  31.  int  Left;      /* leftmost byte in Buffer */
  32.  int  Right;     /* rightmost byte in buffer */
  33.  long Count;     /* # times Reader/Writer called */
  34. } IOstruct;
  35.  
  36. static IOstruct InpControl;
  37. static IOstruct OutControl;
  38.  
  39. int ReaderOpen(Ptr)
  40. char *Ptr;
  41. {/* open input file */
  42.  InpControl.Left = 0;
  43.  InpControl.Right = 0;
  44.  InpControl.Count = 0;
  45.  InpControl.FilePtr = fopen(Ptr,"rb");
  46.  if(InpControl.FilePtr==NULL)
  47.    {printf("Cannot open '%s'\n",Ptr);
  48.     return(FALSE);
  49.    }
  50.  return(TRUE);
  51. }
  52.  
  53. int Reader()
  54. {char Byte;
  55.  if(InpControl.Left==InpControl.Right)
  56.     {/* read next buffer */
  57.      InpControl.Left = 0;
  58.      InpControl.Right = fread(&InpControl.Buffer,1,BUFFER_SIZE,InpControl.FilePtr);
  59.      if(InpControl.Right<=0) return(EOF);
  60.     }
  61.  /* return next byte */
  62.  Byte = InpControl.Buffer[InpControl.Left++];
  63.  InpControl.Count++;
  64.  return(0x00ff&Byte);
  65. }
  66.  
  67. long ReaderCount()
  68. {/* return bytes read */
  69.  return(InpControl.Count);
  70. }
  71.  
  72. void ReaderClose()
  73. {/* close input file */
  74.  fclose(InpControl.FilePtr);
  75. #if 0
  76. printf("%ld bytes read\n",InpControl.Count);
  77. #endif
  78. }
  79.  
  80. int WriterOpen(Ptr)
  81. char *Ptr;
  82. {/* open output file */
  83.  OutControl.Left = 0;
  84.  OutControl.Right = 0;
  85.  OutControl.Count = 0;
  86.  OutControl.FilePtr = fopen(Ptr,"wb");
  87.  if(OutControl.FilePtr==NULL)
  88.    {printf("Cannot open '%s'\n",Ptr);
  89.     return(FALSE);
  90.    }
  91.  return(TRUE);
  92. }
  93.  
  94. int Writer(Byte)
  95. char Byte;
  96. {int Code;
  97.  OutControl.Count++;
  98.  if((OutControl.Count&0x0fff)==0) putchar('.');
  99.  OutControl.Buffer[OutControl.Right++] = Byte;
  100.  if(OutControl.Right==BUFFER_SIZE)
  101.     {/* read next buffer */
  102.      Code = fwrite(&OutControl.Buffer,1,OutControl.Right,OutControl.FilePtr);
  103.      OutControl.Right = 0;
  104.     }
  105.  return(Code);
  106. }
  107.  
  108. long WriterCount()
  109. {/* return bytes written */
  110.  return(OutControl.Count);
  111. }
  112.  
  113.  
  114. void WriterClose()
  115. {/* flush buffer to disk */
  116.  fwrite(&OutControl.Buffer[OutControl.Left],1,OutControl.Right-OutControl.Left,OutControl.FilePtr);
  117.  /* close output file */
  118.  fclose(OutControl.FilePtr);
  119. }